package mrusanov.ui.management.staff

import javax.swing.{JOptionPane, JButton, JLabel, JPanel}
import net.miginfocom.swing.MigLayout
import mrusanov.localization.Localization
import mrusanov.staff.ClubEmployee
import java.util.Date
import java.awt.event.{ActionEvent, ActionListener}
import mrusanov.team.Club

abstract class EmployeePanel(club:Club,employee:Option[ClubEmployee], now:Date, captionKey:String) extends JPanel {
  setLayout(new MigLayout("fill"))
  createComponents()

  def addSingleLabel(msgKey:String) {
    add(new JLabel(Localization(msgKey)), "span 2, wrap")
  }

  def addLabel(msgKey:String, value:Any) {
    add(new JLabel(Localization(msgKey)))
    add(new JLabel(value.toString), "wrap")
  }



  private def createComponents() {
    removeAll()
    addSingleLabel(captionKey)

    employee match {
      case Some(emp) => {
        addLabel("staffPanel.name", emp.name)
        addLabel("staffPanel.age", emp.age(now))
        addLabel("staffPanel.level", emp.level)
      }

      case None => {}
    }
    buttons foreach (add(_, "width 40%"))
  }

  // TODO add contract properties

  def buttons:List[JButton] = {
    employee match {
      case Some(emp) => {
        val dismissButton = new JButton("staffPanel.dismiss")
        dismissButton.addActionListener(new ActionListener {
          def actionPerformed(e: ActionEvent) {
            val answer = JOptionPane.showConfirmDialog(EmployeePanel.this,
              Localization("staffPanel.areYouSureToTerminateContract", emp.moneyForContractTermination(now)),
              Localization("staffPanel.dismiss"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)

            if (answer == JOptionPane.YES_OPTION) {
              emp.terminateContract(now, club)
              createComponents()
            }
          }
        })
        val extendContractButton = new JButton("staffPanel.extendContract")
        List(extendContractButton, dismissButton)
      }
case None => {
val hireButton = new JButton("staffPanel.hire")
List(hireButton)
}
}
}
}
